package br.com.reparoja.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "brand")
public class Brand {

	public Brand() {
		this.equipments = new ArrayList<Equipment>();
	}

	public Brand(String name) {
		this();
		
		this.name = name;
	}

	@Id
	@Column(unique=true)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column
	private String name;

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = Equipment.class)
    @JoinTable(name="equipment_brand", 
    			joinColumns={@JoinColumn(name="brand_id", referencedColumnName="id")}, 
    			inverseJoinColumns={@JoinColumn(name="equipment_id", referencedColumnName="id")}
    )
    private List<Equipment> equipments;
	
	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Equipment> getEquipments() {
		return equipments;
	}

	public void setEquipments(List<Equipment> equipments) {
		this.equipments = equipments;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"id\": ").append(this.id).append(", ");
		sb.append("\"name\": \"").append(this.name).append("\"");
/*
		if (this.equipments.size() > 0) {
			sb.append("\"equipments\": [");
			
			for (Equipment equipment : this.equipments) {
				sb.append(equipment.toString()).append(", ");
			}

			sb.setLength(sb.length() - 2);
			sb.append("]");
		}
*/	
		sb.append("}");
		
		return sb.toString();
	}
	

}
